const pageHelper = require('../../../../../helper/page_helper.js');
const ProjectBiz = require('../../../biz/project_biz.js'); 
const projectSetting = require('../../../public/project_setting.js');
const PassportBiz = require('../../../../../comm/biz/passport_biz.js');
const ActivityBiz = require('../../../biz/activity_biz.js');
const RequestBiz = require('../../../../../comm/biz/request_biz.js');

Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		isLoad: false, 
		user: null
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: async function (options) {
		ProjectBiz.initPage(this);
		let user = PassportBiz.getToken();
		this.setData({ user });
		if (!pageHelper.getOptions(this, options)) return;

		wx.setNavigationBarColor({
			backgroundColor: projectSetting.PROJECT_COLOR,
			frontColor: '#ffffff',
		})
	},

	_loadDetail: async function () {
    let req = RequestBiz.request('/api/ysjl/activity/activityInfo', {id: this.data.id, userId: this.data.user ? this.data.user.id : ''});
		req.then(res => {
			if (res.data.ret == 0) {
        let activity = res.data.data;
        let addressGeo = {
          address: '',
          latitude: activity.addressLat,
          longitude: activity.addressLon,
          name: ''
        };
        activity.addressGeo = addressGeo;
				this.setData({
					activity: activity,
					isLoad: true
				})
			}
		});

	}, 

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () { },

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {
		this._loadDetail();
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () { },

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () { },

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: async function () {
		await this._loadDetail();
		wx.stopPullDownRefresh();
	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () { },

	bindOpenMapTap: function (e) {
		let address = pageHelper.dataset(e, 'address');
		let geo = pageHelper.dataset(e, 'geo');
		ActivityBiz.openMap(address, geo);
  },
  
	url: function (e) {
		pageHelper.url(e, this);
	}, 

	onPageScroll: function (e) {
		// 回页首按钮
		pageHelper.showTopBtn(e, this);

	},

	onShareAppMessage: function (res) {
		return {
			title: this.data.activity.title,
			imageUrl: this.data.activity.pic
		}
	}
})